package com.web.netalbum.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadTools {
	/**
	 * 将一个photo的图片保存在本地文件夹中
	 * 
	 * @param photo
	 */
	private HttpServletRequest request = null;
	private List<FileItem> items = null;// 保存全部上传内容
	private Map<String, List<String>> params = new HashMap<String, List<String>>();
	private Map<String, FileItem> files = new HashMap<String, FileItem>();
	private int maxSize = 3145728;// 默认上传文件大小为3M

	public FileUploadTools(HttpServletRequest request, int maxSize,
			String tempDir) throws Exception {
		// 传递request对象，最大上传限制，临时保存目录
		this.request = request;
		DiskFileItemFactory factory = new DiskFileItemFactory();
		if (tempDir != null) {
			factory.setRepository(new File(tempDir));
		}
		ServletFileUpload upload = new ServletFileUpload(factory);
		if (maxSize > 0) {
			this.maxSize = maxSize;
		}
		upload.setFileSizeMax(maxSize);
		try {
			this.items = upload.parseRequest(request);
		} catch (FileUploadException e) {
			throw e;
		}
		this.init();
	}

	public void init() {
		Iterator<FileItem> iter = this.items.iterator();
		IPTimeStamp its = new IPTimeStamp(this.request.getRemoteAddr());
		while (iter.hasNext()) {
			FileItem item = iter.next();
			if (item.isFormField()) {
				String name = item.getFieldName();
				String value = item.getString();
				List<String> temp = null;
				if (this.params.containsKey(name)) {
					temp = this.params.get(name);
				} else {
					temp = new ArrayList<String>();
				}
				temp.add(value);
				this.params.put(name, temp);
			} else {
				String fileName = its.getIPTimeRand() + "."
						+ item.getName().split("\\.")[1];
				this.files.put(fileName, item);
			}
		}
	}

	public String getParameter(String name) {
		String ret = null;
		List<String> temp = this.params.get(name);
		if (temp != null) {
			ret = temp.get(0);
		}
		return ret;
	}

	public String[] getParameterValues(String name) {
		String ret[] = null;
		List<String> temp = this.params.get(name);
		if (temp != null) {
			ret = temp.toArray(new String[] {});
		}
		return ret;
	}

	public Map<String, FileItem> getUploadFiles() {
		return this.files;
	}

	public List<String> savaAll(String saveDir) throws IOException {
		List<String> names = new ArrayList<String>();
		if (this.files.size() > 0) {
			Set<String> keys = this.files.keySet();
			Iterator<String> iter = keys.iterator();
			File saveFile = null;
			InputStream input = null;
			OutputStream out = null;
			while (iter.hasNext()) {
				FileItem item = this.files.get(iter.next());
				String fileName = new IPTimeStamp(this.request.getRemoteAddr())
						.getIPTimeRand() + "." + item.getName().split("\\.")[1];
				saveFile = new File(saveDir + fileName);
				names.add(fileName);
				try {
					input = item.getInputStream();
					out = new FileOutputStream(saveFile);
					int temp = 0;
					byte data[] = new byte[512];
					while ((temp = input.read(data, 0, 512)) != -1) {
						out.write(data);
					}
				} catch (IOException e) {
					throw e;
				} finally {
					try {
						input.close();
						out.close();
					} catch (IOException e1) {
						throw e1;
					}
				}
			}

		}

		return names;
	}

}
